package com.feibi.imember_printer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;

import ZPL.ZPLPrinterHelper;
import io.flutter.plugin.common.MethodChannel;

public class HT300Util {
    private ZPLPrinterHelper zplPrinterHelper;

    public void connect(Context context, String strIP, String strPort, MethodChannel.Result result) {
        zplPrinterHelper = ZPLPrinterHelper.getZPL(context);
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    if (zplPrinterHelper != null) zplPrinterHelper.PortClose();
                    if (zplPrinterHelper.PortOpen("WiFi," + strIP + "," + strPort) != 0) {
//                        result.error("400", "連接失敗", null);
                        fail(result,"402","失敗");
                    } else {
//                        result.success(true);
                        success(result);
                    }
                } catch (Exception e) {
                    fail(result,"402","失敗"+e.getMessage());
//                    result.error("401", "連接失敗:" + e.getMessage(), null);
                }
            }
        }.start();
    }

    public void disconnect(Context context, MethodChannel.Result result) {
        if (!ZPLPrinterHelper.IsOpened()) {
            result.error("400", "請連接打印機", "");
            return;
        }
        try {
            if (zplPrinterHelper == null)
                zplPrinterHelper = ZPLPrinterHelper.getZPL(context);
            zplPrinterHelper.PortClose();
//            result.success(true);
            success(result);
        } catch (Exception e) {
            result.error("401", e.getMessage(), null);
        }
    }


    public void isConnect(MethodChannel.Result result) {
        result.success(ZPLPrinterHelper.IsOpened());
    }

    public void printText(Context context, String text, MethodChannel.Result result) {
        String x_coordinate = "50"; //起始座標x
        String y_coordinate = "0";//起始座標y
        int formatfont = 7;  //字体（0~6：中文无效，7：中文）。
        int x_multiplication = 5; //字體大小 1：10px。 2：20px。 3：30px。 4：40px。 5：50px。 6：60px。

        if (!ZPLPrinterHelper.IsOpened()) {
            result.error("400", "請連接打印機", "");
            return;
        }

        if (zplPrinterHelper == null)
            zplPrinterHelper = ZPLPrinterHelper.getZPL(context);

        try {
            text = text.replace("\n", "\r\n");
            if (text.length() == 0) {
                result.error("400", "文本為空", null);
                return;
            }
            zplPrinterHelper.start();
            zplPrinterHelper.printData("^CI14\r\n");

            zplPrinterHelper.PW("1200");
            zplPrinterHelper.LS("50"); //左偏移
            zplPrinterHelper.TB("N", "1100", "1600");

            //s2：方向 *N = normal *R = rotate 90 degrees clockwise *I = invert 180 degrees *B = read from bottom up-270 degrees
            int a = zplPrinterHelper.printText(x_coordinate, y_coordinate, formatfont, "N", x_multiplication, text);
            zplPrinterHelper.end();
//            result.success(true);
            success(result);
        } catch (Exception e) {
//            result.error("402", "失敗", e.getMessage());
            fail(result,"402","失敗"+e.getMessage());
        }

    }

    public void printImage(Context context, String filePath, int width, MethodChannel.Result result) {
        if (!ZPLPrinterHelper.IsOpened()) {
            result.error("400", "請連接打印機", "");
            return;
        }
        if (zplPrinterHelper == null)
            zplPrinterHelper = ZPLPrinterHelper.getZPL(context);

        try {
            Bitmap bmp = BitmapFactory.decodeFile(filePath).copy(Bitmap.Config.ARGB_8888, true);
            int bmpWidth = bmp.getWidth();
            int bmpHeight = bmp.getHeight();
            int h = (int) ((width * bmpHeight) / bmpWidth);
            Bitmap dst = Bitmap.createScaledBitmap(bmp, width, h, false);

            int maxWidth = 1200;
            int maxHeight = 1800;
            zplPrinterHelper.start();
            zplPrinterHelper.PW("1200");
            int left = (1200 - width) / 2; //左偏移
            zplPrinterHelper.LS("" + left);
            zplPrinterHelper.printBitmap("0", "50", dst);
            zplPrinterHelper.end();
//                    result.success(true);
            success(result);
        } catch (Exception e) {
//                    result.error("402", "失敗", e.getMessage());
            fail(result,"402","失敗"+e.getMessage());
        }

//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//            }
//        }.start();
    }

    public void setXY(Context context, int x, int y, MethodChannel.Result result) {
        if (!ZPLPrinterHelper.IsOpened()) {
            result.error("400", "請連接打印機", "");
            return;
        }
        if (zplPrinterHelper == null)
            zplPrinterHelper = ZPLPrinterHelper.getZPL(context);

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    zplPrinterHelper.start();
                    zplPrinterHelper.setXY("" + x, "" + y);//设置坐标
                    zplPrinterHelper.end();
//                    result.success(true);
                    success(result);
                } catch (Exception e) {
//                    result.error("402", "失敗", e.getMessage());
                    fail(result,"402","失敗"+e.getMessage());
                }
            }
        }.start();
    }

    public void LH(Context context, int x, int y, MethodChannel.Result result) {
        if (!ZPLPrinterHelper.IsOpened()) {
            result.error("400", "請連接打印機", "");
            return;
        }
        if (zplPrinterHelper == null)
            zplPrinterHelper = ZPLPrinterHelper.getZPL(context);

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    zplPrinterHelper.start();
                    zplPrinterHelper.LH("" + x, "" + y);//设置标签原点位置
                    zplPrinterHelper.end();
//                    result.success(true);
                    success(result);
                } catch (Exception e) {
//                    result.error("402", "失敗", e.getMessage());
                    fail(result,"402","失敗"+e.getMessage());
                }
            }
        }.start();
    }



    public void success(MethodChannel.Result result) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                result.success(true);
            }
        });
    }

    public void fail(MethodChannel.Result result, String code, String message) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                result.error(code, message, "");
            }
        });
    }

}

